package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyTheme;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@Controller
@RequestMapping("strategy")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyThemeService strategyThemeService;

    @Autowired
    private IStrategyCatalogService strategyCatalogService;

    //页面数据和分页
    @RequestMapping("/list")
    public String List(Model model, @ModelAttribute("qo") StrategyQuery qo) {
        Page<Strategy> page = strategyService.query(qo);
        model.addAttribute("page", page);
        return "strategy/list";
    }


    /**
     * 添加页面
     * @param model:共享数据
     * @param id:回显数据的id
     * @return
     */
    @RequestMapping("/input")
    public String input(Model model,String id) {
        List<StrategyTheme> themes = strategyThemeService.list();
        model.addAttribute("themes", themes);
        //分类
        model.addAttribute("catalogs",strategyCatalogService.queryGroup());
        //编辑回显数据
        if(StringUtils.hasLength(id)){
            Strategy strategy = strategyService.get(id);
            model.addAttribute("strategy",strategy);
        }
        return "strategy/input";
    }


    //删除
    @GetMapping("/delete")
    @ResponseBody
    public Object delete(String id) {
        strategyService.delete(id);
        return JsonResult.success();
    }


    //添加或修改
    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Strategy strategy){
        strategyService.saveOrUpdate(strategy);
        return JsonResult.success();
    }
}
